package com.leon.notepad.content;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.leon.notepad.R;
import com.leon.notepad.calendarprovider.calendar.CalendarEvent;
import com.leon.notepad.calendarprovider.calendar.CalendarProviderManager;
import com.leon.notepad.content.utils.DateUtils;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddRemindActivity extends BaseActivity {

    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.et_description)
    EditText etDescription;
    @BindView(R.id.et_event_location)
    EditText etEventLocation;
    @BindView(R.id.et_start_time)
    TextView etStartTime;
    @BindView(R.id.bt_add)
    TextView btAdd;

    private TimePickerView pvTime;
    private long mStartTime=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_remind);
        ButterKnife.bind(this);

        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2029, 11, 28);
        //时间选择器
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                TextView btn = (TextView) v;
                btn.setText(DateUtils.getTimes(date));
                mStartTime = date.getTime();
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .isCenterLabel(true)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(16)//字号
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(null)
                .build();

    }

    @OnClick({R.id.et_start_time, R.id.bt_add})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.et_start_time:
                //点击组件的点击事件
                if (pvTime!=null){
                    pvTime.show(etStartTime);
                }
                break;
            case R.id.bt_add:
                String title = etTitle.getText().toString();
                String desc = etDescription.getText().toString();
                String location = etEventLocation.getText().toString();
                if ("".equals(title)) {
                    Toast.makeText(AddRemindActivity.this,"请输入标题",Toast.LENGTH_SHORT).show();
                    return;
                }else if ("".equals(desc)) {
                    Toast.makeText(AddRemindActivity.this,"请输入描述",Toast.LENGTH_SHORT).show();
                    return;
                }else if ("".equals(location)) {
                    Toast.makeText(AddRemindActivity.this,"请输入地点",Toast.LENGTH_SHORT).show();
                    return;
                }else if (mStartTime==0) {
                    Toast.makeText(AddRemindActivity.this,"请选择开始时间",Toast.LENGTH_SHORT).show();
                    return;
                }

                CalendarEvent calendarEvent = new CalendarEvent(
                        title,
                        desc,
                        location,
                        mStartTime,
                        mStartTime + 60000,
                        0, null
                );

                // 添加事件
                int result = CalendarProviderManager.addCalendarEvent(this, calendarEvent);
                if (result == 0) {
                    Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                } else if (result == -1) {
                    Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
                } else if (result == -2) {
                    Toast.makeText(this, "没有权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}